// Project 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

#define DEBUG
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x) \
	if(! (x)) \
{\
	cerr << "Failed at: "<< #x << endl; \
	cerr << "On line: " <<  __LINE__ << endl; \
	cerr << "In file: " << __FILE__ << endl; \
	abort(); \
}
#endif

typedef vector <int> IVEC;

class picture {
	public:
		void pad(int , int);        // padding
		void rot(int);              // rotate
		void crop(int,int,int,int); // crop
		void panel(int, int);       // panel 

		vector <IVEC> p;
		int rows;
		int cols;
};

void picture::panel(int r, int c) {
	int i, j, board_rows, board_cols;
	vector <IVEC> board;

	ASSERT(r > 0 && c > 0);

	board_rows = p.size() * r;
	board_cols = p[0].size() * c;

	board.resize(board_rows);

	for (i = 0; i < board_rows; i++) {
		//row is i%p.size() ; col is j%p[0].size()
		board[i].resize(board_cols);
		for (j = 0; j < board_cols; j++) {
			board[i][j] = p[i%p.size()][j%p[0].size()];
		}
	}
	p = board;
}

void picture::pad(int w, int pv)
{
	int i, j, k;
	vector <IVEC> tmp;

	ASSERT(w >= 0);
	ASSERT(pv >= 0 && pv <= 255);
	tmp.resize(p.size() + 2 * w);
	for(i = 0; i < w; i++)
	{
		for(j = 0; j < p[0].size() + (w * 2); j++)
		{
			tmp[i].push_back(pv);
			ASSERT(tmp[i][j] == pv);
		}
	}
	k = 0;
	for(i = w; i < p.size() + w; i++)
	{
		for(j = 0; j < w; j++)
		{
			tmp[i].push_back(pv);
			ASSERT(tmp[i][j] == pv);
		}
		for(j = 0; j < p[0].size(); j++)
		{
			tmp[i].push_back(p[k][j]);
			ASSERT(tmp[i][tmp[i].size() - 1] == p[k][j]);
		}
		for(j = 0; j < w; j++)
		{
			tmp[i].push_back(pv);
			ASSERT(tmp[i][tmp[i].size() -1] == pv);
		}
		k++;
	}
	for(i = w + p.size(); i < p.size() + (w * 2); i++)
	{
		for(j = 0; j < p[0].size() + (2 * w); j++)
		{
			tmp[i].push_back(pv);
			ASSERT(tmp[i][j] == pv);
		}
	}

    p = tmp;
    ASSERT(p==tmp);
}

void picture::rot(int leftOrRight)
{
    vector <IVEC> tmp;
    ASSERT(leftOrRight ==1 || leftOrRight==-1);
    int index=0;
	// rotate right
    if(leftOrRight==1){
        for(int i=0;i<cols;i++,index++){
            tmp.push_back(vector <int> () );
            for(int k=rows-1;k>=0;k--){
                tmp[index].push_back(p[k][i]);
                ASSERT(tmp[index][tmp[index].size()-1]==p[k][i]);
            }
        }
    }
	// rotate left
    else{
        for(int i=cols-1;i>=0;i--,index++){
            tmp.push_back(vector <int> () );
            for(int k=0;k<rows;k++){
                tmp[index].push_back(p[k][i]);
                ASSERT(tmp[index][tmp[index].size()-1]==p[k][i]);
            }
        }
    }
    p=tmp;
    ASSERT(p==tmp);

}

void picture::crop(int rstart, int cstart, int rtot, int ctot)
{
	vector < vector<int> > newpic(rtot,vector<int>(ctot));
	//make new picture based on the starting position and desire number of cols, rows
	for (int i=0;i<rtot;i++)
		for(int j=0;j<ctot;j++)
		{
			newpic[i][j]=p[rstart+i][cstart+j];
		}
	//set new nrows and ncols; delete image and set to new image
	rows=rtot;
	cols=ctot;
    p=newpic;
}


main(int argc, char **argv)
{
    int i, h, c, r, j, v, w, pv;
    string s;
    picture *pp;

    // --------------- INITIALIZATION ------------------
    pp = new picture;
    if (!(cin >> s)) {cerr << "Empty file." << endl;  exit(1);}
    if (s != "P2") {cerr << "First word is not P2." << endl; exit(1);}
    if (!(cin >> c) || c <= 0) {cerr << "Bad column spec." << endl; exit(1);}
    if (!(cin >> r) || r <= 0) {cerr << "Bad column spec." << endl; exit(1);}
    if (!(cin >> i) || i != 255) {cerr << "Bad spec of 255." << endl; exit(1);}
    pp -> p.resize(r);
    for (i = 0; i < r; i++) for (j = 0; j < c; j++) {
        if (!(cin >> v) || v < 0 || v > 255) {cerr << "Bad pixel." << endl; exit(1);}
        pp -> p[i].push_back(v);
    }
    if (cin >> s) {cerr << "Extra stuff at the end of the file." << endl; exit(1);}
    pp->rows=pp->p.size();
    pp->cols=pp->p[0].size();
    // -------------------------------------------------

	// retrieve command from command line
    s = argv[1];
    if(s == "Pad")
    {
        w = atoi(argv[2]);
        pv = atoi(argv[3]);
        pp -> pad(w, pv);
    }
    else if(s=="Rot"){
        w = atoi(argv[2]);
        pp->rot(w);
    }
    else if(s=="Crop"){
        w=atoi(argv[2]);
        h=atoi(argv[3]);
        r=atoi(argv[4]);
        c=atoi(argv[5]);
        pp->crop(r,c,h,w);
    }
    else if(s=="Panel"){
        r = atoi(argv[2]);
        c = atoi(argv[3]);
        pp->panel(r,c);
    }
    /*
    else if(){
      continue;
    }
	*/





    /* -------------- PRINT IMAGE TO STDOUT ---------------*/
	cout << "P2\n" << pp-> p.size() << " " << pp-> p[0].size() << "\n255\n";
    for(i = 0; i < pp -> p.size(); i++)
    {
        for(j = 0; j < pp -> p[0].size(); j++)
        {
			cout << pp-> p[i][j] << " ";
//            printf("%4d", pp-> p[i][j]);
        }
        cout << endl;
    }
}
